package com.huika.dealontop.control.pay.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.huika.dealontop.GlobalApp;
import com.huika.dealontop.R;
import com.huika.dealontop.config.Constants;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.BaseAct;
import com.huika.dealontop.control.pay.adapter.PaySuccessLvAdapter;
import com.huika.dealontop.control.pay.utils.BasePay;
import com.huika.dealontop.support.domain.OmisePayResultBean;
import com.huika.dealontop.support.domain.UserModel;
import com.huika.dealontop.support.event.PayEvent;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.utils.DateTimeTool;
import com.huika.dealontop.utils.DebugTool;
import com.huika.dealontop.views.ListViewForScrollView;
import com.huika.dealontop.views.TitleBarHelper;

import de.greenrobot.event.EventBus;

/**
 * Description:支付成功页面
 * Created by zhouweiyong on 2015/11/20.
 */
public class PaySuccessActivity extends BaseAct {
    private TextView tv_order_num_aps, tv_phone_aps, tv_validity_aps;
    private ListViewForScrollView lv_aps;
    private Button btn_sms_aps;
    private LinearLayout layout_phone_aps;
    private PaySuccessLvAdapter lvAdapter;
    private ScrollView layout_content;
    private OmisePayResultBean omisePayResultBean;
    private String orderId;
    private String paymentOrderId;
    private String phone;

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            omisePayResultBean = intent.getParcelableExtra(Constants.OMISE_PAY_RESULT);
            orderId = intent.getStringExtra(Constants.PAY_ORDERID);
            paymentOrderId = intent.getStringExtra(Constants.PAY_PAYMENTORDERID);
            phone = intent.getStringExtra(BasePay.PHONE);
        }
    }

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_pay_success);
    }

    @Override
    protected void initWidget() {
        TitleBarHelper titleBarHelper = new TitleBarHelper(this, R.string.title_pay_success);
        titleBarHelper.setLeftMsg("");
        titleBarHelper.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        layout_phone_aps = (LinearLayout) findViewById(R.id.layout_phone_aps);
        layout_content = (ScrollView) findViewById(R.id.layout_content);
        tv_order_num_aps = (TextView) findViewById(R.id.tv_order_num_aps);
        tv_phone_aps = (TextView) findViewById(R.id.tv_phone_aps);
        tv_validity_aps = (TextView) findViewById(R.id.tv_validity_aps);
        lv_aps = (ListViewForScrollView) findViewById(R.id.lv_aps);
        btn_sms_aps = (Button) findViewById(R.id.btn_sms_aps);
        lvAdapter = new PaySuccessLvAdapter(this);
        lv_aps.setAdapter(lvAdapter);
        tv_order_num_aps.setText(orderId);
        if (omisePayResultBean != null) {
//            if ("1".equals(getString(R.string.common_lang))) {
//                tv_validity_aps.setText(DateTimeTool.ymdhmsDate(omisePayResultBean.getValidTime()));
//            } else {
//                tv_validity_aps.setText(DateTimeTool.dmyhmsDate(omisePayResultBean.getValidTime()));
//            }
            tv_validity_aps.setText(DateTimeTool.dmyhmsDate(omisePayResultBean.getValidTime()));
            lvAdapter.setGroup(omisePayResultBean.getData());
        }
        UserModel userModel = GlobalApp.userModel;
        if (!TextUtils.isEmpty(phone)) {
            tv_phone_aps.setText(String.format("%s****%s", phone.substring(0, 3), phone.substring(7, 10)));
        } else {
            layout_phone_aps.setVisibility(View.GONE);
            btn_sms_aps.setVisibility(View.GONE);
        }

        btn_sms_aps.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        EventBus.getDefault().post(new PayEvent(0));
        super.onBackPressed();
    }

    @Override
    public void widgetClick(View v) {
        btn_sms_aps.setEnabled(false);
        new CountDownTimer(120000, 1000) {
            public void onTick(long millisUntilFinished) {
                btn_sms_aps.setText(String.format("%ss", String.valueOf(millisUntilFinished / 1000)));
            }

            public void onFinish() {
                btn_sms_aps.setText(getString(R.string.pay_success_btn));
                btn_sms_aps.setEnabled(true);
            }
        }.start();
        reSendTicketMsgNet();
    }


    private void reSendTicketMsgNet() {
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("userID", GlobalApp.getInstance().getUserId());
        ajaxParams.putCommonTypeParam("paymentOrderId", paymentOrderId);
        FormResultRequest<String> request = new FormResultRequest<String>(UrlConstants.RESENDTICKETMSG, new Response.Listener<RequestResult<String>>() {

            @Override
            public void onResponse(RequestResult<String> arg0) {
                DebugTool.i("zwy","");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                DebugTool.i("zwy","");
            }
        },new TypeToken<RequestResult<String>>() {}.getType());
        request.setRequestParams(ajaxParams);
        executeRequest(request);
    }
}
